<?php
require_once("Validator.php");

class EmailFormatValidator extends Validator {
	
	public function __construct($fieldName, $value, $errorMessage) {
		parent::__construct($fieldName, $value, $errorMessage);
	}
	
	/**
	 * Checks if a string is a valid email address.
	 *
	 * @return NULL if the field is valid, a ValidationError if injection 
	 * 		   exploit is detected
	 */
	public function validate() {
		
		$regex = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{2,})+$';
		if ( $this->value==null || !eregi($regex, $this->value) ) {
			return new ValidationError($this->fieldName, $this->value, 
									   $this->errorMessage);
		} else {
			return null;
		}
	}
}

?>